package com.foreknow.service.impl;

import com.foreknow.dao.AdminDao;
import com.foreknow.dao.impl.AdminDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.model.Admin;
import com.foreknow.service.AdminService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class AdminServiceImpl implements AdminService {
    @Override
    public boolean add(Admin admin) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        AdminDaoImpl adminDao = new AdminDaoImpl();
        adminDao.setConnection(conn); // 注入连接
        boolean isRight = false;
        try {
            isRight = adminDao.save(admin);
            //手动提交事务
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback(); //回滚事务
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRight;
    }

    @Override
    public boolean deleteById(int id) {
        return false;
    }

    @Override
    public Admin getQueryById(int id) {
        return null;
    }

    @Override
    public boolean update(Admin admin) {
        return false;
    }

    @Override
    public List<Object> queryAll() {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        AdminDaoImpl adminDao = new AdminDaoImpl();
        adminDao.setConnection(conn); // 注入连接
        return  adminDao.queryAll();
    }


}
